package com.springboot.mongodb.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springboot.mongodb.dao.AccountRepository;
import com.springboot.mongodb.entity.Account;

@Service
public class AccounService {
	
	private AccountRepository repository;
	
	@Autowired
	public void setRepository(AccountRepository repository) {
		this.repository = repository;
	}
	
	public void setMongodbData(){
		List<Account> data = new ArrayList<Account>();
		data.add(new Account("1","jack", 100L));
		data.add(new Account("2","jane", 101L));
		data.add(new Account("3","mike", 102L));
		data.add(new Account("4","kangkang", 104L));
		data.add(new Account("5","kangkang", 105L));
		repository.deleteAll();
		data.stream().forEach(item -> {
			repository.save(item);
		});		
	}
	
	public void getMongodbById(String id){
		System.out.println(repository.getById(id));
	}
	
	public void findMongodbByName(String name){
		List<Account> data = repository.findByName(name);
		data.stream().forEach(item -> {
			System.out.println(item);
		});
	}
	
	
	 
	

}
